package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.odin.solution.contract.oms.HotKeywordListReq;
import com.homedo.odin.solution.contract.oms.HotKeywordListResp;
import com.homedo.odin.solution.service.HotKeywordServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liyi on 2018/5/16.
 */
@Api(description = "热搜关键词", tags = {"HotKeyword"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng")
public class MngHotKeywordController extends BaseController<HotKeywordServiceImpl> {

    @ApiOperation(value = "OMS_获取关键词热搜列表", response = HotKeywordListResp.class, httpMethod = "GET")
    @GetMapping("/hot_words")
    public ResultData getHotKeywords() {
        HotKeywordListResp resp = baseService.queryHotKeywords();
        return ResultData.newInstance(Meta.CODE_200, resp);
    }

    @ApiOperation(value = "OMS_热搜关键词修改", httpMethod = "PUT")
    @PutMapping("/hot_words")
    public ResultData updateHotKeywords(
            @Valid @ApiParam(value = "热门关键词列表", required = true) @RequestBody HotKeywordListReq req,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        baseService.updateHotKeywords(req);
        return ResultData.newInstance(Meta.CODE_200, "修改成功");
    }

}
